package com.sgxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sgxy.entity.DeptInf;
import com.sgxy.entity.EmployeeInf;
import com.sgxy.entity.JobInf;
import com.sgxy.service.DeptInfService;
import com.sgxy.service.EmployeeInfService;
import com.sgxy.service.JobInfService;
import com.sgxy.service.UserInfService;
import com.sgxy.service.impl.JobInfServiceImpl;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeInfController {
    private final EmployeeInfService employeeInfService;
    private final JobInfService jobInfService;
    private final DeptInfService deptInfService;
    @Autowired
    public EmployeeInfController(EmployeeInfService employeeInfService, JobInfService jobInfService, DeptInfService deptInfService) {
        this.employeeInfService = employeeInfService;
        this.jobInfService = jobInfService;
        this.deptInfService = deptInfService;
    }


    //集合根据id查询，模糊查询，分页查询
    @RequestMapping("/selectemployee")
    public ModelAndView selectEmployeeAction(EmployeeInf employeeInf,
                                             @RequestParam(value = "page", defaultValue = "1")Integer page){
        ModelAndView modelAndView = new ModelAndView();
        List<JobInf> jobInfs = jobInfService.queryAllFJobInfs(new JobInf());
        List<DeptInf> deptInfs = deptInfService.queryAllDeptInfs(new DeptInf());
        PageHelper.startPage(page,10);
        List<EmployeeInf> employeeInfs = employeeInfService.queryAllEmployeeInfs(employeeInf);
        PageInfo<EmployeeInf> employeeInfPageInfo = new PageInfo<>(employeeInfs);
        modelAndView.addObject("employeeInfPageInfo",employeeInfPageInfo);
        modelAndView.addObject("employeeInf",employeeInf);
        modelAndView.addObject("jobInfs",jobInfs);
        modelAndView.addObject("deptInfs",deptInfs);
        modelAndView.setViewName("/employee/employee.jsp");
        return modelAndView;
    }
    //添加

    @RequestMapping("/toAddemployee")
    public ModelAndView toAddemployeeAction(){
        ModelAndView modelAndView = new ModelAndView();
        List<JobInf> jobInfs = jobInfService.queryAllFJobInfs(new JobInf());
        List<DeptInf> deptInfs = deptInfService.queryAllDeptInfs(new DeptInf());
        modelAndView.addObject("jobInfs",jobInfs);
        modelAndView.addObject("deptInfs",deptInfs);
        modelAndView.setViewName("/employee/showAddEmployee.jsp");
        return modelAndView;
    }

    @RequestMapping("/addemployee")
    public ModelAndView addEmployeeAction(EmployeeInf employeeInf){
        ModelAndView modelAndView  = new ModelAndView();
        int i = employeeInfService.addEmployeeInf(employeeInf);
        modelAndView.setViewName("redirect:/employee/toAddemployee");
        return modelAndView;
    }
    //更新

    @RequestMapping("/toUpdateemployee")
    public ModelAndView toUpdateemployeeAction(EmployeeInf employeeInf){
        ModelAndView modelAndView = new ModelAndView();
        List<EmployeeInf> employeeInfs = employeeInfService.queryAllEmployeeInfs(employeeInf);
        employeeInf = employeeInfs.get(0);
        List<JobInf> jobInfs = jobInfService.queryAllFJobInfs(new JobInf());
        List<DeptInf> deptInfs = deptInfService.queryAllDeptInfs(new DeptInf());
        modelAndView.addObject("employeeInf",employeeInf);
        modelAndView.addObject("jobInfs",jobInfs);
        modelAndView.addObject("deptInfs",deptInfs);
        modelAndView.setViewName("/employee/showUpdateEmployee.jsp");
        return modelAndView;
    }

    @RequestMapping("/updateemployee")
    public ModelAndView updateemployeeAction(EmployeeInf employeeInf){
        System.out.println(employeeInf);
        ModelAndView modelAndView = new ModelAndView();
        int i = employeeInfService.updateEmployeeInf(employeeInf);
//        if(i>0){
//            modelAndView.setViewName("employee.jsp");
//        }
        modelAndView.setViewName("redirect:/employee/selectemployee");
        return modelAndView;
    }

    @RequestMapping("/deleteemployee")
    public ModelAndView deleteEmployee(String ids){
        ModelAndView modelAndView = new ModelAndView();
        String[] split = ids.split(",");
        List<String> list = new ArrayList<String>();
        for(int i=0;i<split.length;i++){
            list.add(split[i]);
        }
        int i = employeeInfService.deleteEmployeeInf(list);
        modelAndView.setViewName("redirect:/employee/selectemployee");
        return modelAndView;
    }

}
